package com.app.service.impl;

import com.app.mapper.PersonalInformationMapper;
import com.app.model.CustomerModel;
import com.app.model.PersonalInformationModel;
import com.app.model.UserModel;
import com.app.service.CustomerService;
import com.app.service.PersonalInformationService;
import com.app.util.Result;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class PersonalInformationServiceImpl extends ServiceImpl<PersonalInformationMapper, PersonalInformationModel> implements PersonalInformationService {
    @Autowired
    private PersonalInformationMapper personalInformationMapper;
    @Autowired
    private CustomerService customerService;
    /**
     * @param userModel 新增记录之前的个人信息查询接口（只差一条数据）
     * @return
     */
    @Override
    public Result getOneByUid(UserModel userModel) {
        PersonalInformationModel personalInformationModel = personalInformationMapper.selectOne(new QueryWrapper<PersonalInformationModel>()
                .eq("user_id", userModel.getId()));
        if (personalInformationModel == null) {
            personalInformationModel = new PersonalInformationModel();
            CustomerModel customerById = customerService.getCustomerById(userModel.getCId());
            personalInformationModel.setPhone(userModel.getMobile());
            String realName = customerById.getRealName();
            if (realName != null) {
                personalInformationModel.setName(realName);
            }
        }
        return Result.success(personalInformationModel);
    }
}
